<template>
  <div>
    <a-modal v-model:visible="visible" :width="800" title="新增律师合同"  @cancel="cacelModel" :bodyStyle="bodyStyle"  @ok="handleOk">
      <a-form :model="formState"  ref="lawyersAddForm" labelAlign="left" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-row :gutter="16">
        <a-col :span="9" :style="{paddingBottom: '10px'}">
          <a-form-item name="dragger" no-style>
            <a-upload-dragger v-model:fileList="fileList"
                              :max-count="1"
                              :data="{contractType: 'con_lawyer'}"
                              :action="action"
                              :beforeUpload="beforeUpload"
                              @change="postFile"
                              name="file"
                              accept=".pdf">
              <div class="dragger-img">
                <img src="@/assets/images/ResourceContract/file.png" alt="">
              </div>
              <p class="ant-upload-text" :style="{color: '#2C3749'}">将文件拖到此处，或 <span :style="{color: '#0062AD'}">点击上传</span> </p>
              <p class="ant-upload-hint" :style="{color: '#6B7A8B', fontSize: '14px'}" >只支持pdf格式</p>
            </a-upload-dragger>
          </a-form-item>
        </a-col>
        <a-col :span="15">
          <div class="form-item-border">
            <a-form-item label="甲方"  name="partyA" :rules="[{ required: true, message: '请输入甲方' }]">
              <a-input :bordered="false" v-model:value="formState.partyA" placeholder="请输入甲方"/>
            </a-form-item>
          </div>
          <div class="form-item-border">
            <a-form-item label="乙方"  name="partyB" :rules="[{ required: true, message: '请输入乙方' }]">
              <a-input :bordered="false" v-model:value="formState.partyB"  placeholder="请输入乙方"/>
            </a-form-item>
          </div>
          <div class="form-item-border">
            <a-form-item label="合同签订日期" :labelCol="{span: 6}" name="signingDate" :rules="[{ type: 'string', required: true, message: '请输入合同签订日期' }]">
              <a-date-picker :locale="locale" :bordered="false" :style="{width: '100%'}" v-model:value="formState['signingDate']"  value-format="YYYY-MM-DD" />
            </a-form-item>
          </div>
        </a-col>
      </a-row>
      </a-form>
      <template #footer>
        <div class="from-btn-right">
          <a-button key="back" class="border-btn" @click="cacelModel">取消</a-button>
          <a-button key="submit" @click="handleOk" :loading="confirmLoading" class="search-btn1">保存</a-button>
        </div>
      </template>
    </a-modal>
  </div>
</template>

<script setup>
import {getCurrentInstance, reactive, ref, defineEmits} from "vue";
import {message} from "ant-design-vue";
const { proxy  } = getCurrentInstance()
import locale from 'ant-design-vue/es/date-picker/locale/zh_CN';
import 'dayjs/locale/zh-cn';
import {BASE_URL} from "@/config/proxyIp";
const visible = ref(false);
const isSubmit = ref(false)
const emit = defineEmits(['saveMemForm'])
const fileList = ref([])
const action = ref(`${BASE_URL}/con/common/uploadContractWithAnalysis`)
const bodyStyle = ref({})
const labelCol = ref(
    {
      style: {
        width: '60px'
      },
    },
)

const wrapperCol = ref({
  span: 20
})
const formState = reactive({
  contractName: '', // 合同名称
  contractNumber: '', // 合同编号
  partyA: '', // 甲方
  partyB: '', // 乙方
  signingDate: '', // 签订日期
  createDate: '', // 创建时间
  createBy: '', // 创建人
})
const lawyersAddForm = ref()
const showModal = () => {
  visible.value = true
  fileList.value = []
  console.log('formAdd', lawyersAddForm.value)
};
const cacelModel = () =>{
  visible.value = false
  isSubmit.value = false
  fileList.value = []
  lawyersAddForm.value.resetFields()
}
const confirmLoading = ref(false)
const handleOk = () => {
  if(formState.filePath) {
    lawyersAddForm.value.validateFields().then(() => {
      console.log('上传的formData', formState);
      confirmLoading.value = true
      proxy.$api.postAddLawyer(formState).then(res => {
        if (res.resultCode === 0) {
          visible.value = false;
          isSubmit.value = true
          confirmLoading.value = false
          emit('saveMemForm', isSubmit.value)
          message.success('新增律师合同成功')
          lawyersAddForm.value.resetFields()
        }
      })
    }).catch(err => {
      console.log('请输入', err)
    })
  } else {
    message.error('请上传律师合同')
  }
}
const postFile = (info) => {
  console.log('上传文件1', info)
  const status = info.file.status
  if (status === 'done') {
    formState.filePath = info.file.response.resultObject.path
  } else if (status === 'error') {
    message.error(`${info.file.name} 文件上传失败`);
  }
}
const beforeUpload = (file) => {
  console.log('上传文件2', file)
  let houzui = file.name.substring(file.name.lastIndexOf('.'), file.name.length).toLowerCase()
  let res = ['.pdf'].includes(houzui)
  if (!res) {
    fileList.value = []
    message.error('请上传 PDF 文件')
  }
  return res
}
// eslint-disable-next-line no-undef
defineExpose({
  showModal,
  handleOk
})
</script>

<style lang="scss" scoped>
.form-item-flex {
  display: flex;
  justify-content: space-between;
  .form-item-border {
    margin-right: 4px;
  }
}
.from-btn-right {
  height: 62px;
  line-height: 62px;
  background: #ffffff;
 // box-shadow: 0px -4px 9px 0px rgba(0,0,0,0.09);
}
.dragger-img {
  img {
    width: 140px;
    height: 110px;
  }
}
</style>
